package com.itheima.web;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@WebServlet("/BaseServlet")
public class BaseServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //获取方法名
            String methodName = request.getParameter("method");
            //获取大class对象
            Class classObj = this.getClass();
            //用class对象调用方法(有可能有私有方法 所以需要获取所有方法)
            Method method = classObj.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            //解除权限
            method.setAccessible(true);
            //执行方法
            method.invoke(classObj.newInstance(),request,response);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) {
            e.printStackTrace();
        }
    }
}
